package com.da.web.sys_user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.sys_role.entity.SysRole;
import com.da.web.sys_role.service.SysRoleService;
import com.da.web.sys_user.entity.PageParm;
import com.da.web.sys_user.entity.SysUser;
import com.da.web.sys_user.service.SysUserService;
import com.da.web.sys_user_role.entity.SysUserRole;
import com.da.web.sys_user_role.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author da
 * @time 2023/12/1 上午 10:21
 */
@RestController
@RequestMapping("/api/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    //新增用户
    @PostMapping
    public Result addUser(@RequestBody SysUser user) {
        //判断账户是否被占用
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUsername, user.getUsername());
        SysUser one = sysUserService.getOne(query);
        if (one != null) {
            return Result.error("账户被占用!");
        }
        user.setIsAdmin("O");
        user.setCreateTime(new Date());
        //使用md5加密密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        //入库处理
        sysUserService.add(user);
        return Result.success("新增用户成功!");
    }

    //编辑用户
    @PutMapping
    public Result editUser(@RequestBody SysUser user) {
        //判断账户是否被占用
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.lambda().eq(SysUser::getUsername, user.getUsername());
        SysUser one = sysUserService.getOne(query);
        if (one != null && !Objects.equals(one.getUserId(), user.getUserId())) {
            return Result.error("账户被占用!");
        }
        //设置更新时间
        user.setUpdateTime(new Date());
        //更新处理
        sysUserService.edit(user);
        return Result.success("编辑用户成功!");
    }

    //删除用户
    @DeleteMapping("/{userId}")
    public Result deleteUser(@PathVariable("userId") Long userId) {
        boolean remove = sysUserService.removeById(userId);
        if (remove) {
            return Result.success("删除成功!");
        }
        return Result.error("删除失败!");
    }

    //列表查询
    @GetMapping("/list")
    public Result getList(PageParm parm) {
        IPage<SysUser> list = sysUserService.list(parm);
        //不显示密码到前端
        list.getRecords().forEach(i -> i.setPassword(""));
        return Result.success("查询成功", list);
    }

    //查询角色列表
    @GetMapping("/roleList")
    public Result getRoleList() {
        List<SysRole> list = sysRoleService.list();
        return Result.success("查询成功", list);
    }

    //查询用户对应的角色
    @GetMapping("/getRoleByUserId")
    public Result getRoleByUserId(Long userId) {
        QueryWrapper<SysUserRole> query = new QueryWrapper<>();
        query.lambda().eq(SysUserRole::getUserId, userId);
        SysUserRole one = sysUserRoleService.getOne(query);
        return Result.success("查询成功", one);
    }

}
